<?php
/**
@Project: 
@Page: 
@Path: 
@Notes: 
**/

class SHML_Base {

	protected $attributes = array();
	protected $classes = array();
	protected $dead_tags = array();
	protected $dx = 0;
	protected $fetched = FALSE;
	protected $innerHTML = NULL;
	protected $printed = FALSE;
	protected $regex = '/<((\w+):(\w+(\-?\w?)*)(\s*[^>]*))\/?>((.*?)<\/\\2:\\3>)?/si';
	protected $tag_regex = NULL;
	protected $var_list = array();
	
	public $cache_dir = NULL;
	public $tag_dir = "/var/www/vhosts/shared/shml/";
	public $template = NULL;
	public $template_dir = NULL;

	public function assign($name, $val="")
	{
		if (is_array($name)) {
			foreach ($name as $k => $v) {
				$this->var_list[$k] = $v;
			}
		}
		else {
			$this->var_list[$name] = $val;
		}
	}
	
	protected function get_attributes($attributes)
	{
		$this->attributes = array();
		$regex = '/(\w+)=(["|\'])(.*?)(\\2)/';
		preg_match_all($regex, $attributes, $match);
		foreach ($match[0] as $k => $v) {
			$attr = trim($match[1][$k]);
			$val = trim($match[3][$k]);
			$this->attributes[$attr] = $val;
		}
		$this->attributes['cache_dir'] = $this->cache_dir;
		$this->attributes['template_dir'] = $this->template_dir;
	}
	
	public function parse($txt="")
	{
		if (!$this->fetched) {
			$this->template = @file_get_contents($this->template);
			$this->fetched = TRUE;
		}
		
		$txt = ($txt == "") ? $this->template_dir . $this->template : $txt;
		preg_match_all($this->regex, $txt, $match);
		foreach ($match[0] as $k => $v) {
			$prefix = strtolower(trim($match[2][$k]));
			$tag = strtolower(trim($match[3][$k]));
			$attributes = trim($match[5][$k]);
			$this->get_attributes($attributes);
			$this->innerHTML = trim($match[7][$k]);
			$this->tag_regex = '/<((' . $prefix . '):(' . $tag . ')(\s*[^>]*))\/?>((.*?)<\/\\2:\\3>)?/si';
			$this->reparse($this->innerHTML);

			$tag_found = FALSE;
			$method = $prefix . "_" . str_replace("-","_",$tag);
			$class = "SHML_" . ucwords(strtolower($prefix));
			if (@file_exists($this->tag_dir . "class." . strtolower(str_replace("_","-",$class)) . ".php")) {
				if (!array_key_exists($class, $this->classes)) {
					$this->classes[$class] = new $class;
				}
				if (method_exists($this->classes[$class], $method)) {
					$tmp = $this->classes[$class]->$method(array("attributes"=>$this->attributes, "var_list"=>$this->var_list, "innerHTML"=>$this->innerHTML));
					$this->replace($tmp);
					$tag_found = TRUE;
					if ($method == "utility_include") { $this->reparse($this->template); }
				}
			}
			
			if ($tag_found === FALSE) {
				$this->dx += 1;
				$this->dead_tags[$this->dx] = TRUE;
				$this->template = preg_replace('/(' . $prefix . ')(:)(' . $tag . ')/si', "\$1{{--" . $this->dx . "--}}$3", $this->template, 1);
			}

		}
		$this->write();
	}
	
	protected function reparse($txt)
	{
		if (preg_match($this->regex, $txt)) {
			$this->parse($txt);
			exit;
		}
	}
	
	protected function replace($r)
	{
		$this->template = preg_replace($this->tag_regex, $r, $this->template, 1);
	}
	
	protected function write()
	{
		$this->reparse($this->template);
		if ($this->printed === FALSE) {
			$this->printed = TRUE;
			if ($this->dx > 0) {
				foreach ($this->dead_tags as $k => $v) {
					$this->template = str_replace("{{--$k--}}", ":", $this->template);
				}	
			}
			print $this->template;
		}
	}
}
?>